/*
 * Copyright 2011 Christoph Ludwig (christoph[at]chludwig.de). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.chludwig.ehumanities.provenance.utils;

import java.io.File;

import javax.servlet.ServletContext;

import de.chludwig.ehumanities.provenance.exception.BrokenConfigurationException;

/**
 * Retrieves data specific to the provenance application from the servlet
 * context.
 */
public class ProvenanceContextUtil {

  /**
   * Key of the data directory init parameter set in the web.xml file.
   */
  public static final String DE_CHLUDWIG_EHUMANITIES_PROVENANCE_DATADIRECTORY = "de.chludwig.ehumanities.provenance.datadirectory";

  /**
   * The application's data directory.
   * 
   * @param ctx the servlet context
   * @return the directory where the provenance application stores all containers and archives
   */
  public static File getDataDirectory(ServletContext ctx) {
    String dataDirectoryParam = ctx.getInitParameter(DE_CHLUDWIG_EHUMANITIES_PROVENANCE_DATADIRECTORY);
    if (dataDirectoryParam == null) {
      String msg = "servlet init parameter " + DE_CHLUDWIG_EHUMANITIES_PROVENANCE_DATADIRECTORY + " not set";
      throw new BrokenConfigurationException(msg, null);
    }
    else {
      return new File(dataDirectoryParam);
    }
  }
}
